//
//  TZJWelcomeViewController.m
//  KeepAccounts
//
//  Created by honeyrr on 2017/9/30.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "KAWelcomeViewController.h"
#import "KATabBarViewController.h"

@interface KAWelcomeViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong) UIScrollView *sv;
@property(nonatomic,strong) UIPageControl *pc;
@property(nonatomic,strong) UIButton *intoBtn;

@end

@implementation KAWelcomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpUI];
}

-(void) setUpUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    for (int i = 0; i < KAImageCount; i++) {
        NSString *imageName = [NSString stringWithFormat:@"welcome%02d", i + 1];
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
        imageView.frame = CGRectMake(i * KAScreenW, 0, KAScreenW, KAScreenH);
        [self.sv addSubview:imageView];
    }
    [self.view addSubview:self.sv];
    [self.view addSubview:self.pc];
    [self.view addSubview:self.intoBtn];
}

#pragma mark - scrollView、pageControl、intoBtn lazy load
-(UIScrollView *)sv
{
    if (!_sv) {
        UIScrollView *sv = [[UIScrollView alloc]initWithFrame:self.view.bounds];
        // UIScrollView 相关属性
        sv.bounces = NO;
        sv.backgroundColor = [UIColor blueColor];
        sv.showsVerticalScrollIndicator = NO;
        sv.showsHorizontalScrollIndicator = NO;
        sv.pagingEnabled = YES;
        sv.contentSize = CGSizeMake(KAImageCount * sv.bounds.size.width, 0);
        sv.delegate = self;
        _sv = sv;
    }
    return _sv;
}

-(UIPageControl *)pc
{
    if (!_pc) {
        /*分页控件与ScrollView无任何关系，是两个独立的控件*/
        UIPageControl *pc = [[UIPageControl alloc]init];
        // UIPageControl 属性设置
        pc.numberOfPages = KAImageCount;
        //控件尺寸
        CGSize size = [pc sizeForNumberOfPages:KAImageCount];
        pc.bounds = CGRectMake(0, 0, size.width, size.height);
        pc.center = CGPointMake(self.view.center.x, KAScreenH - 64);
        //设置颜色
        pc.pageIndicatorTintColor = [UIColor lightGrayColor];
        pc.currentPageIndicatorTintColor = [UIColor whiteColor];
        pc.currentPage = 0;
        _pc = pc;
    }
    return _pc;
}

-(UIButton *)intoBtn
{
    if (!_intoBtn) {
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
        btn.center = self.pc.center;
        [btn setTitle:@"进入" forState:UIControlStateNormal];
        btn.hidden = YES;
        [self.view addSubview:btn];
        btn.layer.borderWidth = 0.8;
        btn.layer.borderColor = [UIColor whiteColor].CGColor;
        btn.layer.cornerRadius = 16;
        [btn addTarget:self action:@selector(intoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.layer.masksToBounds = YES;
        _intoBtn = btn;
    }
    return _intoBtn;
}

-(void) intoBtnClick:(UIButton *)button
{
    [self presentViewController:[KATabBarViewController new] animated:YES completion:nil];
}

#pragma mark - 滚动视图停下来，修改页面控件的小点（页数）
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //计算页数
    int page = scrollView.contentOffset.x / scrollView.bounds.size.width;
    self.pc.currentPage = page;
    //当page到最后一页，隐藏pageControl，显示进入btn
    if (page == 2) {
        self.pc.hidden = YES;
        self.intoBtn.hidden = NO;
    } else {
        self.pc.hidden = NO;
        self.intoBtn.hidden = YES;
    }
}

@end
